/**
 * @(#)TaskCalculateTimer.java 2017年02月27日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.timer;

import javax.annotation.Resource;

import org.jsbd.boss.service.ITaskRecordService;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 任务执行记录
 *
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class TaskCalculateTimer extends TaskAdapterHandler {
    public static final Logger LOGGER = LoggerFactory.getLogger(TaskCalculateTimer.class);

    @Resource
    private ITaskRecordService taskRecordService;

    /*
     * (non-Javadoc)
     *
     * @see org.jsbd.boss.timer.TaskHandler#execute()
     */
    @Override
    public void run() {
        long expireTime = 300;
        Object refreshTime = cacheParamManager.getParamValue(CacheKey.TASK_CALCULATE_TIME);
        if (refreshTime != null) {
            expireTime = Long.parseLong(refreshTime.toString());
        }
        if (redisClient.evalSetNx(CacheKey.T_C_LOCK, expireTime)) {
            long now = System.currentTimeMillis();
            taskRecordService.updateRecords();
            LOGGER.warn("任务计算更新花费时间{}秒", ((System.currentTimeMillis() - now) / 1000));
        }
    }

}
